package com.example.ssoreport.utils;

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import static com.example.ssoreport.utils.EncryptUtils.base64DecodeB;
import static com.example.ssoreport.utils.EncryptUtils.base64Encode;

/**
 * @author xlwang55
 * @date 2023/2/28 14:57
 */
public class RasEncryUtils {



    /**-------------------------- 非对称加密RSA ----------------------------------*/
    /**
     * 随机生成RSA密钥对
     *
     * @return privateKey, publicKey
     * @throws NoSuchAlgorithmException
     */
    public static Map<String, String> genRSAKeyPair() throws NoSuchAlgorithmException {
        // KeyPairGenerator类用于生成公钥和私钥对，基于RSA算法生成对象
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        // 初始化密钥对生成器，密钥大小为96-1024位
        keyPairGen.initialize(1024, new SecureRandom());
        // 生成一个密钥对，保存在keyPair中
        KeyPair keyPair = keyPairGen.generateKeyPair();
        // 得到私钥
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        // 得到公钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        String publicKeyString = base64Encode(publicKey.getEncoded());
        // 得到私钥字符串
        String privateKeyString = base64Encode(privateKey.getEncoded());
        // 将公钥和私钥保存到Map
        Map<String, String> result = new HashMap<String, String>();
        result.put("publicKey", publicKeyString.replaceAll("\n", "").replace("\r", "").trim());
        result.put("privateKey", privateKeyString.replaceAll("\n", "").replace("\r", "").trim());

        return result;
    }

    /**
     * rsa 加密
     *
     * @param str
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String rsaEncrypt(String str, String publicKey) throws Exception {
        //base64编码的公钥
        byte[] decoded = base64DecodeB(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = base64Encode(cipher.doFinal(str.getBytes("UTF-8")));
        return outStr;
    }

    /**
     * rsa解密
     *
     * @param str
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String rsaDecrypt(String str, String privateKey) throws Exception {
        //64位解码加密后的字符串
        byte[] inputByte = base64DecodeB(str);
        //base64编码的私钥
        byte[] decoded = base64DecodeB(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }

}
